<?php

/**
 * Comic form base class.
 *
 * @method Comic getObject() Returns the current form's model object
 *
 * @package    biblioteca
 * @subpackage form
 * @author     Ignacio Calvo
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 24051 2009-11-16 21:08:08Z Kris.Wallsmith $
 */
abstract class BaseComicForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                 => new sfWidgetFormInputHidden(),
      'titulo'             => new sfWidgetFormInputText(),
      'es_en_color'        => new sfWidgetFormInputText(),
      'numero_serie'       => new sfWidgetFormInputText(),
      'total_serie'        => new sfWidgetFormInputText(),
      'notas'              => new sfWidgetFormTextarea(),
      'coleccion_comic_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('ColeccionComic'), 'add_empty' => true)),
      'editor_id'          => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Editor'), 'add_empty' => true)),
      'coloreador_id'      => new sfWidgetFormInputText(),
      'entintador_id'      => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Entintador'), 'add_empty' => true)),
      'lugar_id'           => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Lugar'), 'add_empty' => true)),
      'deudor_id'          => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Deudor'), 'add_empty' => true)),
      'dibujantes_list'    => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Autor')),
      'guionistas_list'    => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Autor')),
      'generos_list'       => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Genero')),
    ));

    $this->setValidators(array(
      'id'                 => new sfValidatorDoctrineChoice(array('model' => $this->getModelName(), 'column' => 'id', 'required' => false)),
      'titulo'             => new sfValidatorString(array('max_length' => 100)),
      'es_en_color'        => new sfValidatorInteger(),
      'numero_serie'       => new sfValidatorInteger(array('required' => false)),
      'total_serie'        => new sfValidatorInteger(array('required' => false)),
      'notas'              => new sfValidatorString(array('required' => false)),
      'coleccion_comic_id' => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('ColeccionComic'), 'required' => false)),
      'editor_id'          => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Editor'), 'required' => false)),
      'coloreador_id'      => new sfValidatorInteger(array('required' => false)),
      'entintador_id'      => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Entintador'), 'required' => false)),
      'lugar_id'           => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Lugar'), 'required' => false)),
      'deudor_id'          => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Deudor'), 'required' => false)),
      'dibujantes_list'    => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Autor', 'required' => false)),
      'guionistas_list'    => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Autor', 'required' => false)),
      'generos_list'       => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Genero', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('comic[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Comic';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['dibujantes_list']))
    {
      $this->setDefault('dibujantes_list', $this->object->Dibujantes->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['guionistas_list']))
    {
      $this->setDefault('guionistas_list', $this->object->Guionistas->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['generos_list']))
    {
      $this->setDefault('generos_list', $this->object->Generos->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveDibujantesList($con);
    $this->saveGuionistasList($con);
    $this->saveGenerosList($con);

    parent::doSave($con);
  }

  public function saveDibujantesList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['dibujantes_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Dibujantes->getPrimaryKeys();
    $values = $this->getValue('dibujantes_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Dibujantes', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Dibujantes', array_values($link));
    }
  }

  public function saveGuionistasList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['guionistas_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Guionistas->getPrimaryKeys();
    $values = $this->getValue('guionistas_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Guionistas', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Guionistas', array_values($link));
    }
  }

  public function saveGenerosList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['generos_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Generos->getPrimaryKeys();
    $values = $this->getValue('generos_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Generos', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Generos', array_values($link));
    }
  }

}
